﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("TST_ALL")]

namespace StockInfoLoader.Utils
{
    internal class ExtractorHelper
    {

        public enum ExtractResults
        {
            Success,
            Error
        };
        // Maybe ftp. Not sure if I need this one
        public static ExtractResults ExtractFTP(string ftpAddress, out string extractedData)
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpAddress);

                request.KeepAlive = true;
                request.UsePassive = true;
                request.UseBinary = true;

                request.Method = WebRequestMethods.Ftp.DownloadFile;
                //request.Credentials = new NetworkCredential(username, password);

                // Read the file from the server & write to destination                
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            extractedData = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO: logging
                //_log.Fatal(string.Format("Message {0} \r\n StackTrace:\r\n {1}\r\n", ex.Message, ex.StackTrace));
                extractedData = string.Format("Message {0} \r\n StackTrace:\r\n {1}\r\n", ex.Message, ex.StackTrace);
                return ExtractResults.Error;
            }
            return ExtractResults.Success;
        }


        public static ExtractResults ExtractHTTP(string ftpAddress, out string extractedData)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ftpAddress);

                request.KeepAlive = true;

                request.Method = WebRequestMethods.File.DownloadFile;
                //request.Credentials = new NetworkCredential(username, password);

                // Read the file from the server & write to destination                
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) // Error here
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            extractedData = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO: logging into the file 
                //_log.Fatal(string.Format("Message {0} \r\n StackTrace:\r\n {1}\r\n", ex.Message, ex.StackTrace));
                extractedData = string.Format("Message {0} \r\n StackTrace:\r\n {1}\r\n", ex.Message, ex.StackTrace);
                return ExtractResults.Error;
            }
            return ExtractResults.Success;
        }
    }
}
